﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EtlComm;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;

namespace ETLHandler
{
    class CMySQLExtract:IExtracter
    {
        #region IExtracter 成员

        public string ip { get; set; }

        public int TaskID { get; set; }

        public string StationID { get; set; }

        public string PrimaryKey { get; set; }

        private int _MaxId;

        public DataTable GetResult(string strSQL)
        {
            DataTable dt = null;
            using (IDbConnection c = new MySqlConnection(ip))
            {
                c.Open();
                using (IDbCommand cmd = c.CreateCommand())
                {
                    using (DbDataAdapter adapter = new MySqlDataAdapter())
                    {
                        cmd.CommandText = strSQL;
                        if (c.State != ConnectionState.Open)
                            throw new Exception("不能打开Interbase数据库");

                        adapter.SelectCommand = cmd as DbCommand;
                        dt = new DataTable();
                        adapter.Fill(dt);
                        dt.Columns.Add(new DataColumn("StationID", typeof(string)) { DefaultValue = StationID });
                        dt.TableName = TaskID.ToString();
                        object val = dt.Compute(string.Format("max({0})", PrimaryKey), "");
                        _MaxId = (val == DBNull.Value ? 0 : (int)val);
                    }
                }
            }
            return dt;
        }

       

        public DateTime GetDBTime()
        {
            using (IDbConnection c = new MySqlConnection(ip))
            {
                using (IDbCommand cmd = c.CreateCommand())
                {
                    cmd.CommandText = "select NOW()";
                    cmd.Connection.Open();
                    if (c.State != ConnectionState.Open)
                        throw new Exception("不能打开SQL Server数据库");
                    DateTime result = (DateTime)cmd.ExecuteScalar();
                    //c.Close();
                    return result;
                }
            }
        }


        public int GetMaxID(string table, string pk)
        {
            if (table == EtlGlobal.DT_MAXID)
            {
                return _MaxId;
            }
            else
            {
                using (IDbConnection c = new MySqlConnection(ip))
                {
                    using (IDbCommand cmd = c.CreateCommand())
                    {
                        cmd.CommandText = string.Format("select max({0}) from {1}", pk, table);
                        cmd.Connection.Open();
                        if (c.State != ConnectionState.Open)
                            throw new Exception("不能打开SQL Server数据库");
                        int result = (int)cmd.ExecuteScalar();
                        //c.Close();
                        return result;
                    }
                }
            }
        }

        #endregion
    }
}
